<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.fileName" placeholder="File Name" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.md5" placeholder="MD5" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.category" placeholder="Category" filterable clearable style="width: 130px" class="filter-item">
        <el-option v-for="item in categories" :key="item.code" :label="item.text" :value="item.code" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">Search</el-button>
      <!-- show column popover -->
      <el-popover placement="bottom" width="150" trigger="click">
        <el-checkbox v-model="showRows.spaceId" class="filter-item" @change="tableKey = tableKey + 1">spaceId</el-checkbox>
        <el-checkbox v-model="showRows.md5" class="filter-item" @change="tableKey = tableKey + 1">md5</el-checkbox>
        <el-checkbox v-model="showRows.parentPath" class="filter-item" @change="tableKey = tableKey + 1">parentPath</el-checkbox>
        <el-checkbox v-model="showRows.physicalAddress" class="filter-item" @change="tableKey = tableKey + 1">physicalAddress</el-checkbox>
        <el-checkbox v-model="showRows.description" class="filter-item" @change="tableKey = tableKey + 1">Description</el-checkbox>

        <el-button slot="reference" v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-setting" />
      </el-popover>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" @sort-change="sortChange">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="File Name">
        <template slot-scope="scope">
          <svg-icon :icon-class="getIcon(scope.row.category)" style="font-size:25px;" />
          <span>{{ scope.row.fileName }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.spaceId" align="center" label="spaceId">
        <template slot-scope="scope">
          <span>{{ scope.row.spaceId }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="File Size">
        <template slot-scope="scope">
          <span>{{ scope.row.fileSize | formatCapacity() }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.md5" align="center" prop="md5" label="MD5" />

      <el-table-column v-if="showRows.parentPath" prop="parentPath" label="Parent Path" show-overflow-tooltip />

      <el-table-column v-if="showRows.physicalAddress" prop="physicalAddress" label="Physical Address" />

      <el-table-column v-if="showRows.description" prop="description" label="Description" show-overflow-tooltip />

      <el-table-column width="180px" align="center" prop="modify_at" sortable="custom" label="UpdateTime">
        <template slot-scope="scope">
          <span>{{ scope.row.modifyAt | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList } from '@/api/usr/logic-file'
import { getSorts } from '@/utils/index'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'LogicFileList',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      showRows: {
        spaceId: true,
        md5: true,
        parentPath: true,
        physicalAddress: false,
        description: false
      },
      list: null,
      total: 0,
      sort: {},
      listLoading: true,
      listQuery: {
        fileName: undefined,
        md5: undefined,
        category: undefined,
        current: 1,
        size: 10,
        asc: undefined,
        desc: undefined
      }
    }
  },
  computed: {
    categories() {
      return this.$store.getters.dictionaries.filter(category => category.dictGroup === 'file_category')
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery)
        .then(response => {
          this.list = response.result.list
          this.total = response.result.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    getIcon(code) {
      if (!code) {
        return 'file-unknown'
      }
      const cate = this.categories.find(category => category.code === code.toString())
      return (cate && cate.reserved1) || 'file-unknown'
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.sort[prop] = undefined
      if (order) {
        this.sort[prop] = order !== 'descending'
      }
      const sort = getSorts(this.sort)
      this.listQuery.asc = sort.asc
      this.listQuery.desc = sort.desc
      this.handleFilter()
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
